import Vue from 'vue'
import App from './App.vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    goodList: [
      {
        name: "诸葛亮",
        price: 1000,
        num: 1,
        checked: false,
      },
      {
        name: "蔡文姬",
        price: 1500,
        num: 1,
        checked: false,
      },
      {
        name: "妲己",
        price: 2000,
        num: 1,
        checked: false,
      },
      {
        name: "鲁班",
        price: 2200,
        num: 1,
        checked: false,
      },
    ],
  },
  mutations: {
    changeChecked(state,index) {
      state.goodList[index].checked = !state.goodList[index].checked 
    },
    addNum(state, index) { 
      state.goodList[index].num += 1
    },
    delNum(state, index) { 
      state.goodList[index].num -= 1
    },
    del(state, index) { 
      state.goodList.splice(index, 1)
    }
  },
  actions: {
    asyncAddNum({commit},index) {
        setTimeout(() => {
          commit('addNum',index)
         },1000)      
    },
    asyncDelNum({commit},index) {
        setTimeout(() => {
          commit('delNum',index)
         },1000)
    },
    asyncDel({commit},index) { 
        setTimeout(() => {
          commit('del',index)
         },1000)
    }
  }
})
Vue.config.productionTip = false

new Vue({
  store,
  render: h => h(App),
}).$mount('#app')
